{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 对话服务"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from openai import OpenAI\n",
    "from IPython import display\n",
    "\n",
    "base_url = \"http://localhost:8000/v1/\"\n",
    "client = OpenAI(api_key=\"EMPTY\", base_url=base_url)\n",
    "\n",
    "def simple_chat(messages, use_stream=False):\n",
    "    return client.chat.completions.create(\n",
    "        model=\"custom_model\",\n",
    "        messages=messages,\n",
    "        stream=use_stream,\n",
    "        max_tokens=4096,\n",
    "        temperature=0.7,\n",
    "        presence_penalty=1.2,\n",
    "        top_p=0.8,\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "messages = [{\n",
    "                \"role\": \"user\",\n",
    "                \"content\": \"你是谁\"\n",
    "            }]\n",
    "\n",
    "use_stream=True\n",
    "\n",
    "response = simple_chat(messages, use_stream)\n",
    "\n",
    "if use_stream:\n",
    "    answer = \"\"\n",
    "    for chunk in response:\n",
    "        answer += chunk.choices[0].delta.content\n",
    "        display.clear_output(wait=True)\n",
    "        print(answer)\n",
    "else:\n",
    "    print(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 语音服务"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 自动语音识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests, json, base64\n",
    "\n",
    "with open(\"statics/examples/audio_test.wav\", 'rb') as audio_file:\n",
    "    \"\"\"\n",
    "    读取文件内容到字节字符串\n",
    "    \"\"\"\n",
    "    audio_bytes = audio_file.read()\n",
    "\n",
    "url=\"http://localhost:8000/v1/asr\"\n",
    "\n",
    "headers = {'Content-Type': 'application/json; charset=utf-8', 'accept': 'application/json'}\n",
    "data=json.dumps({'audio': base64.b64encode(audio_bytes).decode('utf-8')})\n",
    "\n",
    "response = requests.post(url=url, headers=headers, data=data)\n",
    "\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 文本转语音服务"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests, json\n",
    "\n",
    "url=\"http://localhost:8000/v1/tts\"\n",
    "\n",
    "headers = {'Content-Type': 'application/json; charset=utf-8', 'accept': 'application/json'}\n",
    "data=json.dumps({'text': '开发 AI 应用从此简单，汇聚最新最热 AI 模型，提供模型体验、推理、训练、部署和应用的一站式服务，提供充沛算力，做中国最好的 AI 社区'})\n",
    "\n",
    "response = requests.post(url=url, headers=headers, data=data)\n",
    "\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 图片服务"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 文生图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests, json\n",
    "\n",
    "url = \"http://localhost:8000/v1/text2img\"\n",
    "\n",
    "headers = {'Content-Type': 'application/json; charset=utf-8'}\n",
    "data=json.dumps({'prompt': 'a dog, 8K', 'samples': 2, 'output_format': 'base64'})\n",
    "\n",
    "response = requests.post(url=url, headers=headers, data=data)\n",
    "\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 图生图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "init_image = response.json()[\"images\"][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests, json\n",
    "\n",
    "url = \"http://localhost:8000/v1/img2img\"\n",
    "\n",
    "headers = {'Content-Type': 'application/json; charset=utf-8'}\n",
    "data=json.dumps({'init_image': init_image, 'prompt': 'a tigger, 8K', 'samples': 1, 'output_format': 'base64'})\n",
    "\n",
    "response = requests.post(url=url, headers=headers, data=data)\n",
    "\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 局部重绘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import base64\n",
    "\n",
    "def image_to_base64(localfile):\n",
    "    with open(localfile, 'rb') as image_file:\n",
    "        # 读取图像文件内容并编码为Base64\n",
    "        encoded_string = base64.b64encode(image_file.read()).decode('utf-8')\n",
    "    return encoded_string"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests, json\n",
    "\n",
    "url = \"http://localhost:8000/v1/inpainting\"\n",
    "\n",
    "init_image = image_to_base64('statics/examples/image_test.png')\n",
    "mask_image = image_to_base64('statics/examples/mask_image_test.png')\n",
    "\n",
    "headers = {'Content-Type': 'application/json; charset=utf-8'}\n",
    "data=json.dumps({'init_image': f\"data:image;base64,{init_image}\", 'mask_image': f\"data:image;base64,{mask_image}\", 'prompt': 'red jacket', 'samples': 1, 'output_format': 'base64'})\n",
    "\n",
    "response = requests.post(url=url, headers=headers, data=data)\n",
    "\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 多模态理解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import base64\n",
    "\n",
    "def image_to_base64(localfile):\n",
    "    with open(localfile, 'rb') as image_file:\n",
    "        # 读取图像文件内容并编码为Base64\n",
    "        encoded_string = base64.b64encode(image_file.read()).decode('utf-8')\n",
    "    return encoded_string"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests, json\n",
    "\n",
    "url = \"http://localhost:8000/v1/vision/chat\"\n",
    "\n",
    "media = image_to_base64('statics/examples/image_test.png')\n",
    "\n",
    "headers = {'Content-Type': 'application/json; charset=utf-8'}\n",
    "data=json.dumps({'media': media, 'filename': 'image_test.png', 'media_type': 'image', 'text': '请描述一下这幅图'})\n",
    "\n",
    "response = requests.post(url=url, headers=headers, data=data)\n",
    "\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 虚拟试穿"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import base64\n",
    "\n",
    "def image_to_base64(localfile):\n",
    "    with open(localfile, 'rb') as image_file:\n",
    "        # 读取图像文件内容并编码为Base64\n",
    "        encoded_string = base64.b64encode(image_file.read()).decode('utf-8')\n",
    "    return encoded_string"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests, json\n",
    "\n",
    "url = \"http://localhost:8000/v1/tryon\"\n",
    "\n",
    "person_image = image_to_base64('statics/examples/person_image.png')\n",
    "person_mask = image_to_base64('statics/examples/person_mask.png')\n",
    "cloth_image = image_to_base64('statics/examples/cloth_image.png')\n",
    "cloth_mask = image_to_base64('statics/examples/cloth_mask.png')\n",
    "\n",
    "headers = {'Content-Type': 'application/json; charset=utf-8'}\n",
    "data=json.dumps({\n",
    "    'person_image': f\"data:image;base64,{person_image}\",\n",
    "    'person_mask': f\"data:image;base64,{person_mask}\",\n",
    "    'cloth_image': f\"data:image;base64,{cloth_image}\",\n",
    "    'cloth_mask': f\"data:image;base64,{cloth_mask}\",\n",
    "    'num_inference_steps': 30,\n",
    "    'guidance_scale': 2.5,\n",
    "    'seed': 42\n",
    "})\n",
    "\n",
    "response = requests.post(url=url, headers=headers, data=data)\n",
    "\n",
    "response.json()[\"images\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests, json\n",
    "\n",
    "url = \"http://localhost:8000/v1/tryon\"\n",
    "\n",
    "person_image = image_to_base64('checkpoints/wuxlabs/Leffa/examples/person1/01350_00.jpg')\n",
    "person_mask = image_to_base64('statics/examples/person_mask.png')\n",
    "cloth_image = image_to_base64('checkpoints/wuxlabs/Leffa/examples/garment/01486_00.jpg')\n",
    "cloth_mask = image_to_base64('statics/examples/cloth_mask.png')\n",
    "\n",
    "headers = {'Content-Type': 'application/json; charset=utf-8'}\n",
    "data=json.dumps({\n",
    "    'person_image': f\"data:image;base64,{person_image}\",\n",
    "    'person_mask': f\"data:image;base64,{person_mask}\",\n",
    "    'cloth_image': f\"data:image;base64,{cloth_image}\",\n",
    "    'cloth_mask': f\"data:image;base64,{cloth_mask}\",\n",
    "    'num_inference_steps': 30,\n",
    "    'guidance_scale': 2.5,\n",
    "    'seed': 42\n",
    "})\n",
    "\n",
    "response = requests.post(url=url, headers=headers, data=data)\n",
    "\n",
    "response.json()[\"images\"]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "my-mllm-service",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
